/**
 * Created by AlenZhon on 2017/7/12.
 */
$().ready(function(){
    'use strict';
    function repeat(name){      //检查用户名是否与已存在的重合
        for (var i=0;i<localStorage.length;i++){  //调用key方法获取localStorage中数据对应的键名
            //localStorage.key(0)对应data1
            var getKey=localStorage.key(i);        	//通过键名获取值，用户名称进行匹配
            var str=localStorage.getItem(getKey);
            var data=JSON.parse(str);
            if (name===data.username)
            {return true;}
        }

        return false;
    }
    $("div :input").blur(function(){      //添加失去焦点事件
        var $parent = $(this).parent().parent();
        $parent.find("div[id*=tips]").remove();  //删除以前的提醒元素
        //验证用户名
        if ($(this).is('#username')){
            var nameret = /^[0-9a-zA-Z]{6,20}$/;
            if (this.value=== "" ||(this.value!=="" &&!nameret.test(this.value) )) {
                var errorMsg= "请输入正确的用户名";
                $parent.attr("class","weui-cell weui-cell_warn");   //修改为表单报错类
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn">'+errorMsg+'</i>></div>');
            }
            else if (repeat(this.value)){
                errorMsg= "用户名已存在";
                $parent.attr("class","weui-cell weui-cell_warn");
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn"><span class="weui-check__label">'+errorMsg+'</span></i>></div>');
            }
            else {
                $parent.attr("class","weui-cell weui-cell");
                $parent.append('<div class="weui-cell__ft" id="tips onsuccess"><i class="weui-icon-success-circle"></i>></div>');
            }
        }
        //验证邮箱
        if ($(this).is('#email')) {
            var emailret = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
            if (this.value=== "" ||(this.value!=="" &&!emailret.test(this.value) )) {
                $parent.attr("class","weui-cell weui-cell_warn");
                errorMsg= "请输入正确的email地址";
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn"><span class="weui-check__label">'+errorMsg+'</span></i></div>');
            }
            else {
                $parent.attr("class","weui-cell weui-cell");
                $parent.append('<div class="weui-cell__ft" id="tips onsuccess"><i class="weui-icon-success-circle"></i></div>');
            }
        }

        if ($(this).is("#psw1")) {
            var ret = /^[a-zA-Z][A-Za-z0-9_.]{5,19}$/; //由数字、字母、英文句号、下划线组成
            if (this.value === "" || (this.value !== "" && !ret.test(this.value) )) {
                errorMsg = "请输入正确格式的密码";
                $parent.attr("class","weui-cell weui-cell_warn");
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn"><span class="weui-check__label">' + errorMsg + '</span></i></div>');
            }
            else {
                $parent.attr("class","weui-cell weui-cell");
                $parent.append('<div class="weui-cell__ft" id="tips onsuccess"><i class="weui-icon-success-circle"></i>></div>');
                var ret1 = /^[a-zA-Z]{6,20}$/; //仅由字母组成(弱强度)
                var ret2 = /^[a-zA-Z][A-Za-z0-9]{5,19}$/;  //由字母和数字组成（中强度）
                if (ret1.test(this.value)) {
                    $("#pswbar").width("33%").css("background-color","#ff6400");
                    $("#pswstrong").html("弱");
                }
                else if (ret2.test(this.value)) {
                    $("#pswbar").width("66%").css("background-color","#f2f200");
                    $("#pswstrong").html("中");
                }
                else if (ret.test(this.value)) {
                    $("#pswbar").width("100%").css("background-color","#09bb07");
                    $("#pswstrong").html("强");
                }
            }
        }
        //验证重复密码的一致
        if ($(this).is('#psw2')){
            var psw1 = $('#psw1').val();
            if (this.value!==psw1 ) {
                errorMsg= "两次密码请保持一致";
                $parent.attr("class","weui-cell weui-cell_warn");
                $parent.append('<div class="weui-cell__ft" id="tips onerror"><i class="weui-icon-warn"><span class="weui-check__label">'+errorMsg+'</span></i></div>');
            }
            else {
                $parent.attr("class","weui-cell weui-cell");
                $parent.append('<div class="weui-cell__ft" id="tips onsuccess"><i class="weui-icon-success-circle"></i>></div>');
            }
        }

    }).keyup(function () {          //改变文本框的值能即时提醒
        $(this).triggerHandler("blur");
    }).focus(function () {          //定位到文本框上能即时提醒
        $(this).triggerHandler("blur");
    });

    $('#regcheck').click(function(){    //对表单进行整体验证
       $("weui-input").trigger('blur');
       var numerror= $("div[id*=onerror]").length;
       if (numerror){   //表示填写信息有错
           return false;
       }
       else {
           $.toast("注册成功 跳转至登录页面",function () {
               console.log('close');
               var data={};     //将信息存入本地存储
               data.username=$('#username').val();
               data.password=$('#psw1').val();
               data.mail=$('#email').val();
               data.stat=0;
               data.str= JSON.stringify(data);
               localStorage.setItem("userID"+data.username,data.str);
               window.location.href="LogIn.html";    //跳转到登录页面
           });

       }
    });
});
